package cn.edu.ctbu.studentmanage.api;

import cn.edu.ctbu.studentmanage.core.PageUtils;
import cn.edu.ctbu.studentmanage.core.RUtil;
import cn.edu.ctbu.studentmanage.domain.Course;
import cn.edu.ctbu.studentmanage.domain.Score;
import cn.edu.ctbu.studentmanage.service.CourseService;
import cn.edu.ctbu.studentmanage.service.ScoreService;
import cn.edu.ctbu.studentmanage.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api")
@Slf4j
public class ScoreApiController {

    @Autowired
    private ScoreService scoreService;

    /**
     * 读取学生成绩信息./api/scoreindex
     * @return
     */
    @GetMapping("/scoreindex")
    public R findAll(){
        return RUtil.success(scoreService.findAll());
    }

    /**
     * 根据id查找某一成绩信息./api/scoreget/id
     * @param id
     * @return
     */
    @GetMapping("/scoreget/{id}")
    public R get(@PathVariable Integer id) {
        Score score = scoreService.getById(id);

        return RUtil.success(score);
    }

    /**
     * 新增成绩信息./api/scoreinsert
     * @param score
     * @param bindingResult
     */
    @PostMapping("/scoreinsert")
    public R insert(@Valid Score score, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            String errStr = bindingResult.getFieldError().getDefaultMessage();
            log.error(errStr);
        }
        scoreService.Insert(score);

        log.info(score.toString());
        return RUtil.success(null);
    }

    /**
     * 更新学生成绩信息./api/scoreupdate
     * @param score
     */
    @PutMapping ("/scoreupdate")
    public R update(Score score){

        scoreService.update(score);

        log.info(score.toString());
        return RUtil.success(null);
    }

    /**
     * 根据id删除某一成绩信息./api/scoredelete/id
     * @param id
     */
    @DeleteMapping ("/scoredelete/{id}")
    public R delete(@PathVariable Integer id){

        scoreService.delete(id);

//        log.info(Course.toString());
        return RUtil.success(null);
    }

    /**
     *
     * 分页读取全部内容./api/getbypagescore
     * @return
     */
    @GetMapping("/getbypagescore")
    public R findByPage(@RequestParam(value = "page",defaultValue = "0")Integer page,
                                @RequestParam(value = "size",defaultValue = "10")Integer size,
                                @RequestParam(value = "cname",defaultValue = "")String cname,
                                @RequestParam(value = "score",defaultValue = "")String score,
                                @RequestParam(value = "sname",defaultValue = "")String sname
    ){

        Page<Score> scorePage ;
        if(StringUtils.isEmpty(cname) && StringUtils.isEmpty(sname)&& StringUtils.isEmpty(score)
        ){
            Pageable pageable = PageRequest.of(page,size, Sort.by("sno"));
            scorePage =scoreService.findAll(pageable);
        }else {
            Score score1 = new Score();
            score1.setCname(cname);
            score1.setSname(sname);
            score1.setScore(score);


            Pageable pageable = PageRequest.of(page,size,Sort.by("sno"));
            ExampleMatcher matcher =ExampleMatcher.matching()
                    .withMatcher("cname",ExampleMatcher.GenericPropertyMatchers.contains())
                    .withMatcher("score",ExampleMatcher.GenericPropertyMatchers.contains())
                    .withMatcher("sname",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Score> example = Example.of(score1,matcher);
//        Example<Course> example = Example.of(Course);
            scorePage = scoreService.findAll(example,pageable);
        }

        PageUtils pageUtils = new PageUtils(scorePage.getContent(),scorePage.getTotalElements());
        return RUtil.success(pageUtils);
    }
}